//
//  Student.m
//  Json
//
//  Created by apple on 15/8/24.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "Student.h"

@implementation Student

- (NSDictionary *)dictionary
{
    NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
    if (_name)
    {
        mDict[@"name"] = _name;
    }
    else
    {
        mDict[@"name"] = @"";
    }
    mDict[@"age"] = @(_age.intValue);//Number
    if (_sex)
    {
        mDict[@"sex"] = _sex;
    }
    else
    {
        mDict[@"sex"] = @"female";
    }
    if (_books)
    {
        mDict[@"books"] = _books;
    }
    else
    {
        mDict[@"books"] = [NSArray array];
    }
    return [mDict copy];
}
+ (instancetype)studentWithDictionary:(NSDictionary *)dict//将字典转换成对象；
{
    Student *student = [[Student alloc] init];
    student.name = dict[@"name"];
    student.age = [dict[@"age"] stringValue];
    student.books = dict[@"books"];
    student.sex = dict[@"sex"];
    
    return student;
}

//将数组中的元素以逗号连接起来；
- (NSString *)bookString
{
    return [_books componentsJoinedByString:@","];
}

@end
